package InterFaceIModel;

import java.util.ArrayList;
import java.util.List;

import DataBase.DatabaseImplement;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public  class ThuchiModel implements IThuchiModel {

	long id;
	String khoanchi;
	double sotien;
	ILoaiThuChi loaiThuChiModel;
	long ngaythuchi;
	String ghichu;
	private DatabaseImplement databaseImplement;
	@Override
	public long getNgaychi() {
		// TODO Auto-generated method stub
		return ngaythuchi;
	}

	@Override
	public String getGhichu() {
		// TODO Auto-generated method stub
		return ghichu;
	}

	@Override
	public String getKhoanchi() {
		// TODO Auto-generated method stub
		return khoanchi;
	}

	@Override
	public double getSotien() {
		// TODO Auto-generated method stub
		return sotien;
	}

	@Override
	public ILoaiThuChi getLoaiThuChi() {
		// TODO Auto-generated method stub
		return loaiThuChiModel;
	}

	@Override
	public void setNgaychi(long ngaychi) {
		// TODO Auto-generated method stub
		ngaythuchi=ngaychi;
	}

	@Override
	public void setKhoanchi(String khoanchi) {
		// TODO Auto-generated method stub
		this.khoanchi=khoanchi;
	}

	@Override
	public void setSotien(double sotien) {
		// TODO Auto-generated method stub
		this.sotien=sotien;
	}

	@Override
	public void setLoaiThuchi(ILoaiThuChi loaiThuChiModel) {
		// TODO Auto-generated method stub
		this.loaiThuChiModel=loaiThuChiModel;
	}

	@Override
	public void setGhichu(String ghichu) {
		// TODO Auto-generated method stub
		this.ghichu=ghichu;
	}

	@Override
	public long insert(IThuchiModel model, Context context) {
		// TODO Auto-generated method stub
		if (databaseImplement==null) {
			
			databaseImplement=new DatabaseImplement(context);
		}
		ContentValues contentValues=new ContentValues();
		contentValues.put("loaithuchi", model.getLoaiThuChi().getId());
		contentValues.put("tongtien", model.getSotien());
		contentValues.put("ngay", model.getNgaychi());
		contentValues.put("khoanchi", model.getKhoanchi());
		contentValues.put("ghichu", model.getGhichu());
		
		return databaseImplement.InsertCommand(contentValues, "chitieu", null);
	}

	@Override
	public int update(IThuchiModel model, Context context) {
		// TODO Auto-generated method stub
		if (databaseImplement==null) {
			
			databaseImplement=new DatabaseImplement(context);
		}
		ContentValues contentValues=new ContentValues();
		contentValues.put("loaithuchi", model.getLoaiThuChi().getId());
		contentValues.put("tongtien", model.getSotien());
		contentValues.put("ngay", model.getNgaychi());
		contentValues.put("khoanchi", model.getKhoanchi());
		contentValues.put("ghichu", model.getGhichu());
		
		return databaseImplement.UpdateCommand("chitieu", "id=?", new String[]{String.valueOf(model.getId())}, contentValues);
	}
    
	@Override
	public long getId() {
		// TODO Auto-generated method stub
		return id;
	}
  
	@Override
	public void setId(long id) {
		// TODO Auto-generated method stub
		this.id=id;
	}

	@Override
	public List<IThuchiModel> getAll(Context context) {
		// TODO Auto-generated method stub
		if (databaseImplement==null) {
			
			databaseImplement=new DatabaseImplement(context);
		}
		Cursor cursor=databaseImplement.GetAll("chitieu", null, null, null, null, null, null);
		
		cursor.moveToFirst();
		List<IThuchiModel>list=new ArrayList<IThuchiModel>();
		
		while (!cursor.isAfterLast()) {
			
			long Id=cursor.getInt(cursor.getColumnIndex("id"));
			String khoanchi=cursor.getString(cursor.getColumnIndex("khoanchi"));
			String ghichu=cursor.getString(cursor.getColumnIndex("ghichu"));
			long ngaychi=cursor.getLong(cursor.getColumnIndex("ngay"));
			double sotien=cursor.getDouble(cursor.getColumnIndex("tongtien"));
			long idloaithuchi=cursor.getLong(cursor.getColumnIndex("loaithuchi"));
			IThuchiModel thuchiModel=new ThuchiModel();
			thuchiModel.setGhichu(ghichu);
			thuchiModel.setId(Id);
			thuchiModel.setKhoanchi(khoanchi);
			thuchiModel.setNgaychi(ngaychi);
			thuchiModel.setSotien(sotien);
			thuchiModel.setLoaiThuchi(LoaiThuChiModel.getById(idloaithuchi, context));
			list.add(thuchiModel);
			cursor.moveToNext();
			
		}
		return list;
	}

	@Override
	public int delete(Context context, long id) {
		// TODO Auto-generated method stub
		if (databaseImplement==null) {
			
			databaseImplement=new DatabaseImplement(context);
		}
		return databaseImplement.DeleteCommand("chitieu", "id=?", new String[]{String.valueOf(id)});
	}
	

}
